package com.zjx.shop.ware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.shop.common.utils.PageParams;
import com.zjx.shop.common.entity.SkuHasStockVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.ware.dao.WareSkuDao;
import com.zjx.shop.ware.entity.WareSkuEntity;
import com.zjx.shop.ware.service.WareSkuService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(PageParams params) {

        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

        Long wareId = params.getWareId();
        if (wareId != null && wareId != 0){
            wrapper.eq("ware_id",wareId);
        }

        Long skuId = params.getSkuId();
        if (skuId != null && skuId != 0){
            wrapper.eq("sku_id",skuId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                wrapper
        );

        return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
    }

    @Override
    public void addStock(Long skuId, Long wareId, String skuName, Integer skuNum) {
        WareSkuEntity wareSkuEntity = this.baseMapper.selectOne(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntity == null){
            //新增
            wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setStock(skuNum);
        }else {
            wareSkuEntity.setStock(wareSkuEntity.getStock() + skuNum);
        }
        wareSkuEntity.setSkuName(skuName);
        wareSkuEntity.setWareId(wareId);
        wareSkuEntity.setSkuId(skuId);

        this.saveOrUpdate(wareSkuEntity);
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().in("sku_id",skuIds));
        List<WareSkuEntity> list = new ArrayList<>();
        for (Long skuId:skuIds) {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(0);
            list.add(wareSkuEntity);
        }
        for (WareSkuEntity w:wareSkuEntities) {
            for (WareSkuEntity l:list) {
                if (w.getSkuId().equals(l.getSkuId())){
                    l.setStock(w.getStock());
                }
            }
        }

        List<SkuHasStockVo> skuHasStockVos = list.stream().map(wareSkuEntity -> {
            SkuHasStockVo skuHasStockVo =new SkuHasStockVo();
            skuHasStockVo.setSkuId(wareSkuEntity.getSkuId());
            skuHasStockVo.setHasStock(wareSkuEntity.getStock() == 0 ? false : true);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return skuHasStockVos;
    }
}